// Gniazdo.cpp : implementation file
//
#include "stdafx.h"
#include "Gniazdo.h"
#include "FileSenderDoc.h"


// CGniazdo
//extern class CFileSenderView;

CGniazdo::CGniazdo()
{
}

CGniazdo::~CGniazdo()
{
}

// CGniazdo member functions

void CGniazdo::OnAccept(int nErrorCode)
{
	// TODO: Add your specialized code here and/or call the base class

	if (0 != nErrorCode)
	{
		switch( nErrorCode )
		{
			case WSAEADDRINUSE: 
			AfxMessageBox(L"The specified address is already in use.\n");
			break;
			case WSAEADDRNOTAVAIL: 
			AfxMessageBox(L"The specified address is not available from the local machine.\n");
			break;
			case WSAEAFNOSUPPORT: 
			AfxMessageBox(L"Addresses in the specified family cannot be used with this socket.\n");
			break;
			case WSAECONNREFUSED: 
			AfxMessageBox(L"The attempt to connect was forcefully rejected.\n");
			break;
			case WSAEDESTADDRREQ: 
			AfxMessageBox(L"A destination address is required.\n");
			break;
			case WSAEFAULT: 
			AfxMessageBox(L"The lpSockAddrLen argument is incorrect.\n");
			break;
			case WSAEINVAL: 
			AfxMessageBox(L"The socket is already bound to an address.\n");
			break;
			case WSAEISCONN: 
			AfxMessageBox(L"The socket is already connected.\n");
			break;
			case WSAEMFILE: 
			AfxMessageBox(L"No more file descriptors are available.\n");
			break;
			case WSAENETUNREACH: 
			AfxMessageBox(L"The network cannot be reached from this host at this time.\n");
			break;
			case WSAENOBUFS: 
			AfxMessageBox(L"No buffer space is available. The socket cannot be connected.\n");
			break;
			case WSAENOTCONN: 
			AfxMessageBox(L"The socket is not connected.\n");
			break;
			case WSAENOTSOCK: 
			AfxMessageBox(L"The descriptor is a file, not a socket.\n");
			break;
			case WSAETIMEDOUT: 
			AfxMessageBox(L"The attempt to connect timed out without establishing a connection. \n");
			break;
			default:
			TCHAR szError[256];
			wsprintf(szError, L"OnConnect error: %d", nErrorCode);
			AfxMessageBox(szError);
			break;
		}
	}
	else
	{
		m_pDoc->OnAccept();
	}
}

void CGniazdo::OnClose(int nErrorCode)
{
	// TODO: Add your specialized code here and/or call the base class

	m_pDoc->OnClose();
}


void CGniazdo::OnConnect(int nErrorCode)
{
	// TODO: Add your specialized code here and/or call the base class
	if (0 != nErrorCode)
	{
		switch( nErrorCode )
		{
			case WSAEADDRINUSE: 
			AfxMessageBox(L"The specified address is already in use.\n");
			break;
			case WSAEADDRNOTAVAIL: 
			AfxMessageBox(L"The specified address is not available from the local machine.\n");
			break;
			case WSAEAFNOSUPPORT: 
			AfxMessageBox(L"Addresses in the specified family cannot be used with this socket.\n");
			break;
			case WSAECONNREFUSED: 
			AfxMessageBox(L"The attempt to connect was forcefully rejected.\n");
			break;
			case WSAEDESTADDRREQ: 
			AfxMessageBox(L"A destination address is required.\n");
			break;
			case WSAEFAULT: 
			AfxMessageBox(L"The lpSockAddrLen argument is incorrect.\n");
			break;
			case WSAEINVAL: 
			AfxMessageBox(L"The socket is already bound to an address.\n");
			break;
			case WSAEISCONN: 
			AfxMessageBox(L"The socket is already connected.\n");
			break;
			case WSAEMFILE: 
			AfxMessageBox(L"No more file descriptors are available.\n");
			break;
			case WSAENETUNREACH: 
			AfxMessageBox(L"The network cannot be reached from this host at this time.\n");
			break;
			case WSAENOBUFS: 
			AfxMessageBox(L"No buffer space is available. The socket cannot be connected.\n");
			break;
			case WSAENOTCONN: 
			AfxMessageBox(L"The socket is not connected.\n");
			break;
			case WSAENOTSOCK: 
			AfxMessageBox(L"The descriptor is a file, not a socket.\n");
			break;
			case WSAETIMEDOUT: 
			AfxMessageBox(L"The attempt to connect timed out without establishing a connection. \n");
			break;
			default:
			TCHAR szError[256];
			wsprintf(szError, L"OnConnect error: %d", nErrorCode);
			AfxMessageBox(szError);
			break;
		}
		m_pDoc->m_sConnection->Close();
	}
	else
	{
		m_pDoc->OnConnect();
	}
}


void CGniazdo::OnReceive(int nErrorCode)
{
	// TODO: Add your specialized code here and/or call the base class

	m_pDoc->OnReceive();

	CAsyncSocket::OnReceive(nErrorCode);
}


void CGniazdo::OnSend(int nErrorCode)
{
	// TODO: Add your specialized code here and/or call the base class

	m_pDoc->OnSend();

	CAsyncSocket::OnSend(nErrorCode);
}


void CGniazdo::SetParent(CFileSenderDoc *pDoc)
{
	m_pDoc = pDoc;
}
